﻿using System;
using System.Data;
using System.Collections.Generic;
using System.IO;
using BYFW.SFL;

namespace LDSP.BRL
{
    public partial class AccidentCase : BYFW.BOL.BaseEntity
    {
        public AccidentCase()
        {
            m_tableName = "t_accidentCase";
            m_hasName = true;
            m_hasComment = true;
        }

        protected DateTime m_createDate = DateTime.MinValue;
        protected string m_summary = string.Empty;
        protected string m_result = string.Empty;
        protected string m_reference = string.Empty;
        protected int m_typeID = 0;
        protected string m_fileName = string.Empty;
        protected string m_fileExtension = string.Empty;
        protected bool m_isConverted = false;
        protected string m_PDFContent = string.Empty;

        protected AccidentCaseType m_accidentCaseType = null;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateDate
        {
            get { return m_createDate; }
            set { m_createDate = value; }
        }
        /// <summary>
        /// 概述
        /// </summary>
        public string Summary
        {
            get { return m_summary; }
            set { m_summary = value; }
        }
        /// <summary>
        /// 结果
        /// </summary>
        public string Result
        {
            get { return m_result; }
            set { m_result = value; }
        }
        /// <summary>
        /// 类型ID
        /// </summary>
        public int TypeID
        {
            get { return m_typeID; }
            set { m_typeID = value; }
        }
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get { return m_fileName; }
            set { m_fileName = value; }
        }
        /// <summary>
        /// 文件扩展
        /// </summary>
        public string FileExtension
        {
            get { return m_fileExtension; }
            set { m_fileExtension = value; }
        }

        /// <summary>
        /// 参照法规及标准
        /// </summary>
        public string Reference
        {
            get { return m_reference; }
            set { m_reference = value; }
        }
        /// <summary>
        /// 是否转换过PDF文件
        /// </summary>
        public bool IsConverted
        {
            get { return m_isConverted; }
            set { m_isConverted = value; }
        }
        /// <summary>
        /// PDF文件内容
        /// </summary>
        public string PDFContent
        {
            get { return m_PDFContent; }
            set { m_PDFContent = value; }
        }

        public AccidentCaseType AccidentCaseType
        {
            get
            {
                if (m_accidentCaseType == null)
                {
                    m_accidentCaseType = new AccidentCaseType();
                    if (m_typeID != 0)
                    {
                        m_accidentCaseType.GetProperties(m_typeID);
                    }
                }
                return m_accidentCaseType;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("createDate", m_createDate);
            m_container.Add("summary", m_summary);
            m_container.Add("result", m_result);
            m_container.Add("typeID", m_typeID);
            m_container.Add("fileName", m_fileName);
            m_container.Add("fileExtension", m_fileExtension);
            m_container.Add("reference", m_reference);
            m_container.Add("isConverted", m_isConverted ? 1 : 0);
            if (!string.IsNullOrEmpty(m_PDFContent))
            {
                m_container.Add("PDFContent", m_PDFContent);
            }
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_createDate = this.GetDateValue(record, "createDate");
            m_summary = this.GetStringValue(record, "summary");
            m_result = this.GetStringValue(record, "result");
            m_typeID = this.GetIntValue(record, "typeID");
            m_fileName = this.GetStringValue(record, "fileName");
            m_fileExtension = this.GetStringValue(record, "fileExtension");
            m_reference = this.GetStringValue(record, "reference");
            m_isConverted = this.GetBoolValue(record, "isConverted");
            return true;
        }

        public string GetDownloadFileFullName()
        {
            try
            {
                return Path.Combine(LDSP.Util.CommonService.SITEROOT, "Docs", FileExtension);
            }
            catch { return ""; }
        }
        /// <summary>
        /// Delete the file if it exists
        /// </summary>
        private void DeleteFile()
        {
            string fileFullPath = this.GetDownloadFileFullName();
            if (File.Exists(fileFullPath))
            {
                File.Delete(fileFullPath);
            }
        }

        /// <summary>
        /// 删除Instance，删除文件
        /// </summary>
        public override int Delete(string transID)
        {
            this.DeleteFile();
            return base.Delete(transID);
        }

        /// <summary>
        /// 根据filename创建fileextension
        /// </summary>
        public void CreateNewFile(string fileName)
        {
            this.DeleteFile();
            m_fileName = fileName;
            string ext = Path.GetExtension(m_fileName);
            m_fileExtension = Guid.NewGuid().ToString() + ext;
        }

        public override DataTable GetRecords(string transID, string condition)
        {
            string sql = "select " + GetFiled() + " from " + DB.GetCol(TableName) + " " + condition;
            DataTable dt = DB.Select(sql, transID).Tables[0];
            if (!string.IsNullOrEmpty(m_defaultSort))
            {
                CommonOP.SortedTable(dt, m_defaultSort);
            }
            return dt;
        }

        private string GetFiled(string prefix = "")
        {
            return string.Format(m_field, prefix);
        }

        string m_field = @" {0}id
      ,{0}name
      ,{0}comment
      ,{0}createDate
      ,{0}summary
      ,{0}result
      ,{0}typeID
      ,{0}fileName
      ,{0}fileExtension
      ,{0}reference
      ,{0}isConverted ";
    }
}
